import { createRouter, createWebHashHistory } from 'vue-router';
import Login from '../views/login.vue';
import Goods from '../views/goods.vue';
import Goodss from '../views/goodss.vue';
import Goodsin from '../views/goodsin.vue';
import Goodsout from '../views/goodsout.vue';
import Personal from '../views/personal.vue';
import Classa from '../views/class/classa.vue';
import Classb from '../views/class/classb.vue';
import Classc from '../views/class/classc.vue';
import Classd from '../views/class/classd.vue';
import Classe from '../views/class/classe.vue';
import Classf from '../views/class/classf.vue';
import Classg from '../views/class/classg.vue';
import Classh from '../views/class/classh.vue';
import Classi from '../views/class/classi.vue';
import Classj from '../views/class/classj.vue';
import Ininventory from '../views/inventory/ininventory.vue';
import Inorder from '../views/inventory/inorder.vue';
import Outinventory from '../views/inventory/outinventory.vue';
import Outorder from '../views/inventory/outorder.vue';
import Isshelf from '../views/isshelf.vue';
import User from '../views/user.vue';
import Logging from '../views/logging.vue';
import Chart from '../views/chart.vue';
import Goodsstaff from '../views/goodsstaff.vue';

const routes = [
  {
    path: '/',
    redirect: '/login' // 这里应该是路由路径，而不是文件路径
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/goods', // 这是 goods 路由的主路径
    component: Goods,
    meta: { requiresAuth: true }, // 添加认证标记
    children: [
      {
        path: '/chart', // 使用相对路径, 子路径相对于父路径
        component: Chart,
      },
      {
        path: '/personal', // 使用相对路径, 子路径相对于父路径
        component: Personal,
      },
      {
        path: '/goodss', // 使用相对路径, 子路径相对于父路径
        component: Goodss,
      },
      {
        path: '/goodsin', 
        component: Goodsin
      },
      {
        path: '/goodsout', 
        component: Goodsout
      },
      {
        path: '/isshelf',
        component: Isshelf
      },
      {
        path: '/user',
        component: User
      },
      {
        path: '/logging',
        component: Logging
      },
      {
        path: '/ininventory',
        component: Ininventory
      },
      {
        path :'/outinventory',
        component: Outinventory
      },
      {
        path: '/inorder',
        component : Inorder
      },
      {
        path:'/outorder',
        component: Outorder
      },
      {
        path: '/classa',
        component: Classa
      },
      {
        path: '/classb',
        component: Classb
      },
      {
        path: '/classc',
        component: Classc
      },
      {
        path: '/classd',
        component: Classd
      },
      {
        path: '/classe',
        component: Classe
      },
      {
        path: '/classf',
        component: Classf
      },
      {
        path: '/classg',
        component: Classg
      },
      {
        path: '/classh',
        component: Classh
      },
      {
        path: '/classi',
        component: Classi
      },
      {
        path: '/classj',
        component: Classj
      }
    ]
  },
  {
    path: '/goodsstaff', // 这是 goodstaff 路由的主路径
    component: Goodsstaff,
    meta: { requiresAuth: true }, // 添加认证标记
    children: [
      {
        path: '/goodsstaff/goodss', // 使用相对路径, 子路径相对于父路径
        component: Goodss,
      },
      {
        path: '/goodsstaff/chart', // 使用相对路径, 子路径相对于父路径
        component: Chart,
      },
      {
        path: '/goodsstaff/personal', // 使用相对路径, 子路径相对于父路径
        component: Personal,
      },
      {
        path: '/goodsstaff/goodss', // 使用相对路径, 子路径相对于父路径
        component: Goodss,
      },
      {
        path: '/goodsstaff/goodsin', 
        component: Goodsin
      },
      {
        path: '/goodsstaff/goodsout', 
        component: Goodsout
      },
      {
        path: '/goodsstaff/isshelf',
        component: Isshelf
      },
      // {
      //   path: '/user',
      //   component: User
      // },
      {
        path: '/goodsstaff/logging',
        component: Logging
      },
      {
        path: '/goodsstaff/ininventory',
        component: Ininventory
      },
      {
        path :'/goodsstaff/outinventory',
        component: Outinventory
      },
      {
        path: '/goodsstaff/inorder',
        component : Inorder
      },
      {
        path:'/goodsstaff/outorder',
        component: Outorder
      },
      {
        path: '/goodsstaff/classa',
        component: Classa
      },
      {
        path: '/goodsstaff/classb',
        component: Classb
      },
      {
        path: '/goodsstaff/classc',
        component: Classc
      },
      {
        path: '/goodsstaff/classd',
        component: Classd
      },
      {
        path: '/goodsstaff/classe',
        component: Classe
      },
      {
        path: '/goodsstaff/classf',
        component: Classf
      },
      {
        path: '/goodsstaff/classg',
        component: Classg
      },
      {
        path: '/goodsstaff/classh',
        component: Classh
      },
      {
        path: '/goodsstaff/classi',
        component: Classi
      },
      {
        path: '/goodsstaff/classj',
        component: Classj
      }
    ]
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  if (to.matched.some(record => record.meta.requiresAuth) && !token) {
    next('/login'); // 如果用户未登录，重定向到登录页面
  } else {
    next(); // 否则继续导航
  }
});

export default router;
